<?php

namespace app\web\controller;

use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

use QL\QueryList;

use app\common\model\Sogou as SogouModel;
use app\common\model\Baidu as BaiduModel;
use app\common\model\Sm as SmModel;
use app\common\model\AdminUser as AdminUserModel;
/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class Preweb extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();
//        $this->id = (input('ssid','','intval') == '')?input('cookie.ssid','','intval'):input('ssid','','intval');
//      	$model = new AdminUserModel();
//        $this->data = $model->get(['id'=>$this->id]);
//      	 if(!is_null($this->data)){
//            $this->data = $this->data->setting;
//        }else{
//            exit();
//        }
    }



    public function collesogou(){
        //从get中拿 拿不到从cookie拿  拿不到返回空
       
        if ($this->id != '') {
            // 把用户ID写到cookie 用来二次搜索
            cookie('ssid',$this->id);
            $this->model = new SogouModel();
            $data_lists  = $this->model->where(['uid'=>$this->id])->order(['sort' => 'ASC'])->select();
            $default_keyword = $this->data['keyword']; //从数据库中读取

            $html = '';
            $html .= '<link rel="stylesheet" href="http://manger.xinrenrenli.com/sogou/css/style.css" />';
            $html .='<div id="sogou-search">';
            $keyword = input('get.keyword',$default_keyword,'urlencode');
            foreach ($data_lists as $key => $value) {
                $html.= get_style($value,'sogou');
            }
            $html .= '</div>';

            $data = QueryList::get('https://m.sogou.com/web/searchList.jsp?keyword='.$keyword);
            $data = $data->getHtml();
            $data = preg_replace('/<div id="mainBody" class="mainBody">/i','<div id="mainBody" class="mainBody">'.$html,$data);
            $data = preg_replace('/<head>/i','<head><base href="http://m.sogou.com">',$data);

            return $data;
        }else{
            return '';
        }
    }

    public function collebaidu(){
        if ($this->id != '') {
          cookie('ssid',$this->id);
          $this->model = new BaiduModel();
          $data_lists  = $this->model->where(['uid'=>$this->id])->order(['sort' => 'ASC'])->select();
          $default_keyword = $this->data['keyword']; //从数据库中读取

          $html = '';
          $html .= '<link rel="stylesheet" href="http://manger.xinrenrenli.com/baidu/css/style.css" />';
          $html .= '<script src="http://manger.xinrenrenli.com/static/js/swiper-4.1.6.min.js"></script>';
          $html .='<div id="baidu-search">';
          foreach ($data_lists as $key => $value) {
              $html.= get_style($value,'baidu');
          }

          $html .= '</div>';
          $html .= "<script>new Swiper ('.swiper-container', { 
              loop: true, autoHeight:true, 
              autoplay:true, // 如果需要分页器 
              pagination: { el: '.swiper-pagination',}, // 如果需要滚动条 
              scrollbar: { el: '.swiper-scrollbar', } }
              );</script>";
         $keyword = input('get.word',$default_keyword,'urlencode');
         $data = QueryList::get('https://m.baidu.com/s?word='.$keyword);
         $data = $data->getHtml();
         $data = preg_replace('/<div id="results" class="results">/i','<div id="results" class="results">'.$html,$data);


         echo $data;
       }else{
            return '';
       }   
    }

    public function collesm(){

        $this->model = new SmModel();
        $data_lists  = $this->model->where(['uid'=>1])->order(['sort' => 'ASC'])->select();
        $html = '';
        $html .= '<link rel="stylesheet" href="http://bdjc.com/sm/css/style.css" />';
        $html .='<div id="sm-search">';

        foreach ($data_lists as $key => $value) {
            $html .= get_style($value,'sm');
        }

        $html .= '</div>';
       
        $ql = QueryList::getInstance();

        // 安装时需要设置PhantomJS二进制文件路径

        $ql->use(PhantomJs::class,ROOT_PATH.'bin/phantomjs');

        $html = $ql->browser('http://www.sina.com.cn/')->getHtml();

    //    $data = $ql->browser('https://m.sm.cn/s?q=%E4%BA%BA%E6%B5%81%E5%90%8E%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9')->getHtml();

    //     $data = preg_replace('/<\/form><\/div>/i','</form></div>'.$html,$data,1);


         echo $html;
    }

    public function getKeyword(){
        return json_encode([
            'code'=>0,'data'=>iconv("GBK","UTF-8",urldecode(key($_POST))),'msg'=>""
        ]);
    }
}